home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / sdctd.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  8.8 KB  |  293 lines

  1. /* Copyright (C) 1994, 1995, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: sdctd.c,v 1.2 2000/09/19 19:00:49 lpd Exp $ */
  20. /* DCT decoding filter stream */
  21. #include "memory_.h"
  22. #include "stdio_.h"
  23. #include "jpeglib_.h"
  24. #include "jerror_.h"
  25. #include "gdebug.h"
  26. #include "gsmemory.h"        /* for gsmalloc.h */
  27. #include "gsmalloc.h"        /* for gs_memory_default */
  28. #include "strimpl.h"
  29. #include "sdct.h"
  30. #include "sjpeg.h"
  31.  
  32. /* ------ DCTDecode ------ */
  33.  
  34. /* JPEG source manager procedures */
  35. private void
  36. dctd_init_source(j_decompress_ptr dinfo)
  37. {
  38. }
  39. static const JOCTET fake_eoi[2] =
  40. {0xFF, JPEG_EOI};
  41. private boolean
  42. dctd_fill_input_buffer(j_decompress_ptr dinfo)
  43. {
  44.     jpeg_decompress_data *jddp =
  45.     (jpeg_decompress_data *) ((char *)dinfo -
  46.                   offset_of(jpeg_decompress_data, dinfo));
  47.  
  48.     if (!jddp->input_eod)
  49.     return FALSE;        /* normal case: suspend processing */
  50.     /* Reached end of source data without finding EOI */
  51.     WARNMS(dinfo, JWRN_JPEG_EOF);
  52.     /* Insert a fake EOI marker */
  53.     dinfo->src->next_input_byte = fake_eoi;
  54.     dinfo->src->bytes_in_buffer = 2;
  55.     jddp->faked_eoi = true;    /* so process routine doesn't use next_input_byte */
  56.     return TRUE;
  57. }
  58. private void
  59. dctd_skip_input_data(j_decompress_ptr dinfo, long num_bytes)
  60. {
  61.     struct jpeg_source_mgr *src = dinfo->src;
  62.     jpeg_decompress_data *jddp =
  63.     (jpeg_decompress_data *) ((char *)dinfo -
  64.                   offset_of(jpeg_decompress_data, dinfo));
  65.  
  66.     if (num_bytes > 0) {
  67.     if (num_bytes > src->bytes_in_buffer) {
  68.         jddp->skip += num_bytes - src->bytes_in_buffer;
  69.         src->next_input_byte += src->bytes_in_buffer;
  70.         src->bytes_in_buffer = 0;
  71.         return;
  72.     }
  73.     src->next_input_byte += num_bytes;
  74.     src->bytes_in_buffer -= num_bytes;
  75.     }
  76. }
  77. private void
  78. dctd_term_source(j_decompress_ptr dinfo)
  79. {
  80. }
  81.  
  82. /* Set the defaults for the DCTDecode filter. */
  83. private void
  84. s_DCTD_set_defaults(stream_state * st)
  85. {
  86.     s_DCT_set_defaults(st);
  87. }
  88.  
  89. /* Initialize DCTDecode filter */
  90. private int
  91. s_DCTD_init(stream_state * st)
  92. {
  93.     stream_DCT_state *const ss = (stream_DCT_state *) st;
  94.     struct jpeg_source_mgr *src = &ss->data.decompress->source;
  95.  
  96.     src->init_source = dctd_init_source;
  97.     src->fill_input_buffer = dctd_fill_input_buffer;
  98.     src->skip_input_data = dctd_skip_input_data;
  99.     src->term_source = dctd_term_source;
  100.     src->resync_to_restart = jpeg_resync_to_restart;    /* use default method */
  101.     ss->data.common->memory = ss->jpeg_memory;
  102.     ss->data.decompress->dinfo.src = src;
  103.     ss->data.decompress->skip = 0;
  104.     ss->data.decompress->input_eod = false;
  105.     ss->data.decompress->faked_eoi = false;
  106.     ss->phase = 0;
  107.     return 0;
  108. }
  109.  
  110. /* Process a buffer */
  111. private int
  112. s_DCTD_process(stream_state * st, stream_cursor_read * pr,
  113.            stream_cursor_write * pw, bool last)
  114. {
  115.     stream_DCT_state *const ss = (stream_DCT_state *) st;
  116.     jpeg_decompress_data *jddp = ss->data.decompress;
  117.     struct jpeg_source_mgr *src = jddp->dinfo.src;
  118.     int code;
  119.  
  120.     if_debug3('w', "[wdd]process avail=%u, skip=%u, last=%d\n",
  121.           (uint) (pr->limit - pr->ptr), (uint) jddp->skip, last);
  122.     if (jddp->skip != 0) {
  123.     long avail = pr->limit - pr->ptr;
  124.  
  125.     if (avail < jddp->skip) {
  126.         jddp->skip -= avail;
  127.         pr->ptr = pr->limit;
  128.         if (!last)
  129.         return 0;    /* need more data */
  130.         jddp->skip = 0;    /* don't skip past input EOD */
  131.     }
  132.     pr->ptr += jddp->skip;
  133.     jddp->skip = 0;
  134.     }
  135.     src->next_input_byte = pr->ptr + 1;
  136.     src->bytes_in_buffer = pr->limit - pr->ptr;
  137.     jddp->input_eod = last;
  138.     switch (ss->phase) {
  139.     case 0:        /* not initialized yet */
  140.         /*
  141.          * Adobe implementations seem to ignore leading garbage bytes,
  142.          * even though neither the standard nor Adobe's own
  143.          * documentation mention this.
  144.          */
  145.         while (pr->ptr < pr->limit && pr->ptr[1] != 0xff)
  146.         pr->ptr++;
  147.         if (pr->ptr == pr->limit)
  148.         return 0;
  149.         src->next_input_byte = pr->ptr + 1;
  150.         src->bytes_in_buffer = pr->limit - pr->ptr;
  151.         ss->phase = 1;
  152.         /* falls through */
  153.     case 1:        /* reading header markers */
  154.         if ((code = gs_jpeg_read_header(ss, TRUE)) < 0)
  155.         return ERRC;
  156.         pr->ptr =
  157.         (jddp->faked_eoi ? pr->limit : src->next_input_byte - 1);
  158.         switch (code) {
  159.         case JPEG_SUSPENDED:
  160.             return 0;
  161.             /*case JPEG_HEADER_OK: */
  162.         }
  163.         /* If we have a ColorTransform parameter, and it's not
  164.          * overridden by an Adobe marker in the data, set colorspace.
  165.          */
  166.         if (ss->ColorTransform >= 0 &&
  167.         !jddp->dinfo.saw_Adobe_marker) {
  168.         switch (jddp->dinfo.num_components) {
  169.             case 3:
  170.             jddp->dinfo.jpeg_color_space =
  171.                 (ss->ColorTransform ? JCS_YCbCr : JCS_RGB);
  172.             /* out_color_space will default to JCS_RGB */
  173.             break;
  174.             case 4:
  175.             jddp->dinfo.jpeg_color_space =
  176.                 (ss->ColorTransform ? JCS_YCCK : JCS_CMYK);
  177.             /* out_color_space will default to JCS_CMYK */
  178.             break;
  179.         }
  180.         }
  181.         ss->phase = 2;
  182.         /* falls through */
  183.     case 2:        /* start_decompress */
  184.         if ((code = gs_jpeg_start_decompress(ss)) < 0)
  185.         return ERRC;
  186.         pr->ptr =
  187.         (jddp->faked_eoi ? pr->limit : src->next_input_byte - 1);
  188.         if (code == 0)
  189.         return 0;
  190.         ss->scan_line_size =
  191.         jddp->dinfo.output_width * jddp->dinfo.output_components;
  192.         if_debug4('w', "[wdd]width=%u, components=%d, scan_line_size=%u, min_out_size=%u\n",
  193.               jddp->dinfo.output_width,
  194.               jddp->dinfo.output_components,
  195.               ss->scan_line_size, jddp->template.min_out_size);
  196.         if (ss->scan_line_size > (uint) jddp->template.min_out_size) {
  197.         /* Create a spare buffer for oversize scanline */
  198.         jddp->scanline_buffer =
  199.             gs_alloc_bytes_immovable(jddp->memory,
  200.                          ss->scan_line_size,
  201.                      "s_DCTD_process(scanline_buffer)");
  202.         if (jddp->scanline_buffer == NULL)
  203.             return ERRC;
  204.         }
  205.         jddp->bytes_in_scanline = 0;
  206.         ss->phase = 3;
  207.         /* falls through */
  208.     case 3:        /* reading data */
  209.       dumpbuffer:
  210.         if (jddp->bytes_in_scanline != 0) {
  211.         uint avail = pw->limit - pw->ptr;
  212.         uint tomove = min(jddp->bytes_in_scanline,
  213.                   avail);
  214.  
  215.         if_debug2('w', "[wdd]moving %u/%u\n",
  216.               tomove, avail);
  217.         memcpy(pw->ptr + 1, jddp->scanline_buffer +
  218.                (ss->scan_line_size - jddp->bytes_in_scanline),
  219.                tomove);
  220.         pw->ptr += tomove;
  221.         jddp->bytes_in_scanline -= tomove;
  222.         if (jddp->bytes_in_scanline != 0)
  223.             return 1;    /* need more room */
  224.         }
  225.         while (jddp->dinfo.output_height > jddp->dinfo.output_scanline) {
  226.         int read;
  227.         byte *samples;
  228.  
  229.         if (jddp->scanline_buffer != NULL)
  230.             samples = jddp->scanline_buffer;
  231.         else {
  232.             if ((uint) (pw->limit - pw->ptr) < ss->scan_line_size)
  233.             return 1;    /* need more room */
  234.             samples = pw->ptr + 1;
  235.         }
  236.         read = gs_jpeg_read_scanlines(ss, &samples, 1);
  237.         if (read < 0)
  238.             return ERRC;
  239.         if_debug3('w', "[wdd]read returns %d, used=%u, faked_eoi=%d\n",
  240.               read,
  241.               (uint) (src->next_input_byte - 1 - pr->ptr),
  242.               (int)jddp->faked_eoi);
  243.         pr->ptr =
  244.             (jddp->faked_eoi ? pr->limit : src->next_input_byte - 1);
  245.         if (!read)
  246.             return 0;    /* need more data */
  247.         if (jddp->scanline_buffer != NULL) {
  248.             jddp->bytes_in_scanline = ss->scan_line_size;
  249.             goto dumpbuffer;
  250.         }
  251.         pw->ptr += ss->scan_line_size;
  252.         }
  253.         ss->phase = 4;
  254.         /* falls through */
  255.     case 4:        /* end of image; scan for EOI */
  256.         if ((code = gs_jpeg_finish_decompress(ss)) < 0)
  257.         return ERRC;
  258.         pr->ptr =
  259.         (jddp->faked_eoi ? pr->limit : src->next_input_byte - 1);
  260.         if (code == 0)
  261.         return 0;
  262.         ss->phase = 5;
  263.         /* falls through */
  264.     case 5:        /* we are DONE */
  265.         return EOFC;
  266.     }
  267.     /* Default case can't happen.... */
  268.     return ERRC;
  269. }
  270.  
  271. /* Release the stream */
  272. private void
  273. s_DCTD_release(stream_state * st)
  274. {
  275.     stream_DCT_state *const ss = (stream_DCT_state *) st;
  276.  
  277.     gs_jpeg_destroy(ss);
  278.     if (ss->data.decompress->scanline_buffer != NULL)
  279.     gs_free_object(ss->data.common->memory,
  280.                ss->data.decompress->scanline_buffer,
  281.                "s_DCTD_release(scanline_buffer)");
  282.     gs_free_object(ss->data.common->memory, ss->data.decompress,
  283.            "s_DCTD_release");
  284.     /* Switch the template pointer back in case we still need it. */
  285.     st->template = &s_DCTD_template;
  286. }
  287.  
  288. /* Stream template */
  289. const stream_template s_DCTD_template =
  290. {&st_DCT_state, s_DCTD_init, s_DCTD_process, 2000, 4000, s_DCTD_release,
  291.  s_DCTD_set_defaults
  292. };
  293.